import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Date;

public class Casovnik extends JPanel implements KeyListener, MouseListener,
		MouseMotionListener, Runnable {
	Font font = new Font("Arial", Font.BOLD, 14);
	Image pozadinaSata;
	Dimension velicinaProzora;

	public Casovnik() {
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	private void napraviPozadinuSata() {
		pozadinaSata = createImage((int) velicinaProzora.getWidth(),
				(int) velicinaProzora.getHeight());
		Graphics2D g2d = (Graphics2D) pozadinaSata.getGraphics();

		g2d.setRenderingHints(new RenderingHints(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON));
		g2d.setPaint(Color.gray);
		g2d.fill(new Rectangle2D.Double(0, 0, velicinaProzora.getWidth(),
				velicinaProzora.getHeight()));

		AffineTransform t1 = new AffineTransform();
		t1.translate(velicinaProzora.getWidth() / 2,
				velicinaProzora.getHeight() / 2);
		t1.scale(velicinaProzora.getWidth() / 200,
				velicinaProzora.getHeight() / 200);

		g2d.setTransform(t1);

		g2d.setPaint(Color.white);
		g2d.fill(new Ellipse2D.Float(-90, -90, 180, 180));

		g2d.setPaint(Color.black);
		g2d.setStroke(new BasicStroke(1));
		g2d.draw(new Ellipse2D.Float(-90, -90, 180, 180));
		
		//crtanje za milisekunde
		AffineTransform t3=new AffineTransform();
		t3.setTransform(t1);
		t3.translate(0,40);

		g2d.setTransform(t3);

		g2d.setPaint(Color.gray);
		g2d.fill(new Ellipse2D.Float(-20, -20, 40, 40));
		

		g2d.setPaint(Color.black);//za slova
		g2d.setFont(font); 
		for (int i = 1; i <= 12; i++) {
			double ugao = i * 30; 
			AffineTransform t2 = new AffineTransform();
			String s = "" + i;
			FontMetrics fm = g2d.getFontMetrics();
			int sirina = fm.stringWidth(s);
			int visina = fm.getHeight();

			t2.setTransform(t1);//PAZI OVOOOOOOOOOOOOOOOOOOOOOOOOOOO
			t2.rotate(ugao * 3.1415 / 180);
			t2.translate(0, -80);
			t2.rotate(-ugao * 3.1415 / 180);
 
			g2d.setTransform(t2);
			g2d.drawString("" + i, -sirina / 2, visina / 2);

		}
	}

	private void crtajKazaljku(Graphics2D g2d, double v, Color c, int debljina,
			int duzina) {
		AffineTransform t1 = new AffineTransform();
		t1.translate(velicinaProzora.getWidth() / 2,
				velicinaProzora.getHeight() / 2);
		t1.scale(velicinaProzora.getWidth() / 200,
				velicinaProzora.getHeight() / 200);

		double ugao = v * 3.1415 * 2;

		t1.rotate(ugao);
		g2d.setTransform(t1);

		g2d.setStroke(new BasicStroke(debljina));
		g2d.setPaint(c);
		g2d.setRenderingHints(new RenderingHints(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON));
		g2d.draw(new Line2D.Float(0, 0, 0, -duzina));

	}
	private void crtajMiliKazaljku(Graphics2D g2d, double v, Color c, int debljina,
			int duzina) {
		AffineTransform t1 = new AffineTransform();
		t1.translate(velicinaProzora.getWidth() / 2,
				velicinaProzora.getHeight() / 2 +110);
		t1.scale(velicinaProzora.getWidth() / 200,
				velicinaProzora.getHeight() / 200);

		double ugao = v * 3.1415 * 2;

		t1.rotate(ugao);
		g2d.setTransform(t1);

		g2d.setStroke(new BasicStroke(debljina));
		g2d.setPaint(c);
		g2d.setRenderingHints(new RenderingHints(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON));
		g2d.draw(new Line2D.Float(0, 0, 0, -duzina));

	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;

		Dimension d = getSize();
		if (pozadinaSata == null || velicinaProzora.getWidth() != d.getWidth()
				|| velicinaProzora.getHeight() != d.getHeight()) {
			velicinaProzora = d;
			napraviPozadinuSata();
		}

		g2d.drawImage(pozadinaSata, 0, 0, this);

		//Date date = new Date();
		//double sekundi = date.getSeconds() / 60.0;
		//double minuta = (date.getMinutes() + sekundi) / 60.0;
		//double sati = (date.getHours() % 12 + minuta) / 12.0;
		long milliseconds=System.currentTimeMillis();
		double sekundi =((milliseconds / 1000) % 60)/60.0 ;
		double minuta =  ((((milliseconds / (1000*60)) % 60))+sekundi)/60.0;
		double sati   =  (((((milliseconds / (1000*60*60))+1) % 12))+minuta)/12.0;
		crtajKazaljku(g2d, sati, Color.black, 5, 40);
		crtajKazaljku(g2d, minuta, Color.black, 3, 70);
		crtajKazaljku(g2d, sekundi, Color.red, 3, 50);
		
		//crtaj milisekundu
		crtajMiliKazaljku(g2d, (milliseconds%1000)/1000.0, Color.red, 2, 20);
	}

	// KeyListener implementation
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_UP:
			break;
		case KeyEvent.VK_DOWN:
			break;

		case KeyEvent.VK_LEFT:
			break;

		case KeyEvent.VK_RIGHT:
			break;
		}
	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}

	// MouseListener implementation
	public void mouseClicked(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	// MouseMotionListener
	public void mouseMoved(MouseEvent e) {
	}

	public void mouseDragged(MouseEvent e) {
	}

	public void run() {
		long starttime;

		while (true) {
			starttime = System.currentTimeMillis();
			try {
				repaint();
				starttime += 40;
				Thread.sleep(Math.max(0, starttime - System.currentTimeMillis()));
			} catch (InterruptedException e) {
				break;
			}
		}
	}

	public static void main(String s[]) {
		Casovnik panel = new Casovnik();

		panel.setOpaque(true);

		JFrame frame = new JFrame("Casovnik");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		frame.getContentPane().add(panel);
		frame.setSize(new Dimension(800, 600));
		frame.setVisible(true);
		frame.addKeyListener(panel);

		Thread t = new Thread(panel);
		t.start();
	}
}